<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of produtos_marcas_model
 *
 * @author souzadavi
 */
class Pedidos_model extends CI_Model {

    //put your code here

    public function __construct() {
        parent::__construct();
        $this->load->config('parametros');
    }

    public function consultar($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblPedidos'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    public function consultar_total($_idioma) {
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblPedidos'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    public function cadastrar($_dados = array()) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblPedidos'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }

    public function alterar($_id, $_dados = array()) {
        $this->db->where('id', $_id);
        $this->db->set($_dados);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblPedidos'));
        if ($this->db->affected_rows() > 0) {
            return $_id;
        } else {
            return NULL;
        }
    }

    public function excluir($_id) {
        $this->db->where('id', $_id);
        $this->db->delete($this->config->item('dbCliente') . $this->config->item('tblPedidos'));
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return NULL;
        }
    }

    public function consultar_lista($_id = null) {
        if ($_id != null) {
            $this->db->where('pd.id', $_id);
        }
        $this->db->select('pd.*, pe.nome as pe_nome, fe.nome as fe_nome, pa.nome as pa_nome, psi.nome as psi_nome, lp.nome as lp_nome');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblPessoa') . ' as pe ', 'pe.id = pd.pessoa_id', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblPagamentos') . ' as pa ', 'pa.id = pd.ec_pagamentos_id', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblLocalPais') . ' as lp ', 'lp.id = pd.local_pais_id', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblPedidosStatusInfo') . ' as psi ', 'psi.ec_pedidos_status_id = pd.ec_pedidos_status_id AND psi.idiomas_id = 2', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblFrete') . ' as fe ', 'fe.id = pd.ec_frete_id', 'LEFT');
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblPedidos') . ' as pd');

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

}